package han.cirno.corrupt.data.instances

import androidx.room.Entity
import androidx.room.PrimaryKey
import han.cirno.cirno_dao.CirnoDaoAwA
import han.cirno.corrupt.components.calendar.CalendarUtil
import java.util.Calendar
import kotlin.random.Random

object UserExamData {
    @CirnoDaoAwA
    @Entity
    data class Exam(
        val name:String,
        val date:Triple<String,String,String>,
        val fromTime:Pair<String,String>,
        val toTime:Pair<String,String>,
        val regionAndPlace:String,
        val seat:String,
        @PrimaryKey val uid:Long=Random.nextLong()
    ) {
        val isDateUndeclared:Boolean
            get()=date.run {first.isBlank()||second.isBlank()||third.isBlank()}
        val isTimeUndeclared:Boolean
            get()=fromTime.run {first.isBlank()||second.isBlank()}||toTime.run {first.isBlank()||second.isBlank()}
        val isPlaceUndeclared:Boolean
            get()=regionAndPlace==":"

        fun getTimeDelta(currentTimeMils:Long):TimeDelta{
            if (date.run { first.isBlank()||second.isBlank()||third.isBlank() }||
                fromTime.run { first.isBlank()||second.isBlank() }||toTime.run { first.isBlank()||second.isBlank() }){
                return TimeDelta(TimeState.Unspecified)
            }
            val (year, month, day) = date
            val fromDateInMillis= Calendar.getInstance().run {
                val (hour,min)=fromTime
                set(year.toInt(),month.toInt()-1,day.toInt(),hour.toInt(),min.toInt(),0)
                timeInMillis
            }
            val toDateInMillis= Calendar.getInstance().run {
                val (hour,min)=toTime
                set(year.toInt(),month.toInt()-1,day.toInt(),hour.toInt(),min.toInt(),0)
                timeInMillis
            }
            val fromTimeDelta = fromDateInMillis-currentTimeMils
            val toTimeDelta = toDateInMillis-currentTimeMils
            val dayHourMinuteAndSecond = CalendarUtil.getDayHourMinuteAndSecond(fromTimeDelta)
            val dayLeft=dayHourMinuteAndSecond[0]
            val hourLeft=dayHourMinuteAndSecond[1]
            val minLeft=dayHourMinuteAndSecond[2]
            val secondLeft=dayHourMinuteAndSecond[3]
            if (fromTimeDelta>0){
                return TimeDelta(
                    TimeState.Coming,
                    dayLeft,hourLeft,minLeft,secondLeft
                )
            }
            if (toTimeDelta>0){
                val hourMinuteAndSecond= CalendarUtil.getDayHourMinuteAndSecond(toTimeDelta)
                return TimeDelta(
                    TimeState.Progressing,
                    hourMinuteAndSecond[1],
                    hourMinuteAndSecond[2],
                    hourMinuteAndSecond[3]
                )
            }
            return TimeDelta(TimeState.End)
        }

        enum class TimeState{ Unspecified,Coming,Progressing,End }
        data class TimeDelta(
            val timeState:TimeState,
            val timeLeftDay:Int=0,
            val timeLeftHour:Int=0,
            val timeLeftMinute:Int=0,
            val timeLeftSecond:Int=0
        )
    }
}